home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / tests / set.test < prev    next >
Encoding:
Text File  |  1997-08-15  |  7.9 KB  |  234 lines  |  [TEXT/ALFA]

  1. # Commands covered:  set
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1996 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. # SCCS: @(#) set.test 1.6 97/06/23 18:18:54
  13.  
  14. if {[string compare test [info procs test]] == 1} then {source defs}
  15.  
  16. catch {unset x}
  17. catch {unset i}
  18.  
  19. test set-1.1 {TclCompileSetCmd: missing variable name} {
  20.     list [catch {set} msg] $msg
  21. } {1 {wrong # args: should be "set varName ?newValue?"}}
  22. test set-1.2 {TclCompileSetCmd: simple variable name} {
  23.     set i 10
  24.     list [set i] $i
  25. } {10 10}
  26. test set-1.3 {TclCompileSetCmd: error compiling variable name} {
  27.     set i 10
  28.     catch {set "i"xxx} msg
  29.     set msg
  30. } {quoted string doesn't terminate properly}
  31. test set-1.4 {TclCompileSetCmd: simple variable name in quotes} {
  32.     set i 17
  33.     list [set "i"] $i
  34. } {17 17}
  35. test set-1.5 {TclCompileSetCmd: simple variable name in braces} {
  36.     catch {unset {a simple var}}
  37.     set {a simple var} 27
  38.     list [set {a simple var}] ${a simple var}
  39. } {27 27}
  40. test set-1.6 {TclCompileSetCmd: simple array variable name} {
  41.     catch {unset a}
  42.     set a(foo) 37
  43.     list [set a(foo)] $a(foo)
  44. } {37 37}
  45. test set-1.7 {TclCompileSetCmd: non-simple (computed) variable name} {
  46.     set x "i"
  47.     set i 77
  48.     list [set $x] $i
  49. } {77 77}
  50. test set-1.8 {TclCompileSetCmd: non-simple (computed) variable name} {
  51.     set x "i"
  52.     set i 77
  53.     list [set [set x] 2] $i
  54. } {2 2}
  55.  
  56. test set-1.9 {TclCompileSetCmd: 3rd arg => assignment} {
  57.     set i "abcdef"
  58.     list [set i] $i
  59. } {abcdef abcdef}
  60. test set-1.10 {TclCompileSetCmd: only two args => just getting value} {
  61.     set i {one two}
  62.     set i
  63. } {one two}
  64.  
  65. test set-1.11 {TclCompileSetCmd: simple global name} {
  66.     proc p {} {
  67.         global i
  68.         set i 54
  69.         set i
  70.     }
  71.     p
  72. } {54}
  73. test set-1.12 {TclCompileSetCmd: simple local name} {
  74.     proc p {bar} {
  75.         set foo $bar
  76.         set foo
  77.     }
  78.     p 999
  79. } {999}
  80. test set-1.13 {TclCompileSetCmd: simple but new (unknown) local name} {
  81.     proc p {} {
  82.         set bar
  83.     }
  84.     catch {p} msg
  85.     set msg
  86. } {can't read "bar": no such variable}
  87. test set-1.14 {TclCompileSetCmd: simple local name, >255 locals} {
  88.     proc 260locals {} {
  89.         # create 260 locals (the last ones with index > 255)
  90.         set a0 0; set a1 0; set a2 0; set a3 0; set a4 0
  91.         set a5 0; set a6 0; set a7 0; set a8 0; set a9 0
  92.         set b0 0; set b1 0; set b2 0; set b3 0; set b4 0
  93.         set b5 0; set b6 0; set b7 0; set b8 0; set b9 0
  94.         set c0 0; set c1 0; set c2 0; set c3 0; set c4 0
  95.         set c5 0; set c6 0; set c7 0; set c8 0; set c9 0
  96.         set d0 0; set d1 0; set d2 0; set d3 0; set d4 0
  97.         set d5 0; set d6 0; set d7 0; set d8 0; set d9 0
  98.         set e0 0; set e1 0; set e2 0; set e3 0; set e4 0
  99.         set e5 0; set e6 0; set e7 0; set e8 0; set e9 0
  100.         set f0 0; set f1 0; set f2 0; set f3 0; set f4 0
  101.         set f5 0; set f6 0; set f7 0; set f8 0; set f9 0
  102.         set g0 0; set g1 0; set g2 0; set g3 0; set g4 0
  103.         set g5 0; set g6 0; set g7 0; set g8 0; set g9 0
  104.         set h0 0; set h1 0; set h2 0; set h3 0; set h4 0
  105.         set h5 0; set h6 0; set h7 0; set h8 0; set h9 0
  106.         set i0 0; set i1 0; set i2 0; set i3 0; set i4 0
  107.         set i5 0; set i6 0; set i7 0; set i8 0; set i9 0
  108.         set j0 0; set j1 0; set j2 0; set j3 0; set j4 0
  109.         set j5 0; set j6 0; set j7 0; set j8 0; set j9 0
  110.         set k0 0; set k1 0; set k2 0; set k3 0; set k4 0
  111.         set k5 0; set k6 0; set k7 0; set k8 0; set k9 0
  112.         set l0 0; set l1 0; set l2 0; set l3 0; set l4 0
  113.         set l5 0; set l6 0; set l7 0; set l8 0; set l9 0
  114.         set m0 0; set m1 0; set m2 0; set m3 0; set m4 0
  115.         set m5 0; set m6 0; set m7 0; set m8 0; set m9 0
  116.         set n0 0; set n1 0; set n2 0; set n3 0; set n4 0
  117.         set n5 0; set n6 0; set n7 0; set n8 0; set n9 0
  118.         set o0 0; set o1 0; set o2 0; set o3 0; set o4 0
  119.         set o5 0; set o6 0; set o7 0; set o8 0; set o9 0
  120.         set p0 0; set p1 0; set p2 0; set p3 0; set p4 0
  121.         set p5 0; set p6 0; set p7 0; set p8 0; set p9 0
  122.         set q0 0; set q1 0; set q2 0; set q3 0; set q4 0
  123.         set q5 0; set q6 0; set q7 0; set q8 0; set q9 0
  124.         set r0 0; set r1 0; set r2 0; set r3 0; set r4 0
  125.         set r5 0; set r6 0; set r7 0; set r8 0; set r9 0
  126.         set s0 0; set s1 0; set s2 0; set s3 0; set s4 0
  127.         set s5 0; set s6 0; set s7 0; set s8 0; set s9 0
  128.         set t0 0; set t1 0; set t2 0; set t3 0; set t4 0
  129.         set t5 0; set t6 0; set t7 0; set t8 0; set t9 0
  130.         set u0 0; set u1 0; set u2 0; set u3 0; set u4 0
  131.         set u5 0; set u6 0; set u7 0; set u8 0; set u9 0
  132.         set v0 0; set v1 0; set v2 0; set v3 0; set v4 0
  133.         set v5 0; set v6 0; set v7 0; set v8 0; set v9 0
  134.         set w0 0; set w1 0; set w2 0; set w3 0; set w4 0
  135.         set w5 0; set w6 0; set w7 0; set w8 0; set w9 0
  136.         set x0 0; set x1 0; set x2 0; set x3 0; set x4 0
  137.         set x5 0; set x6 0; set x7 0; set x8 0; set x9 0
  138.         set y0 0; set y1 0; set y2 0; set y3 0; set y4 0
  139.         set y5 0; set y6 0; set y7 0; set y8 0; set y9 0
  140.         set z0 0; set z1 0; set z2 0; set z3 0; set z4 0
  141.         set z5 0; set z6 0; set z7 0; set z8 0; set z9 1234
  142.     }
  143.     260locals
  144. } {1234}
  145. test set-1.15 {TclCompileSetCmd: variable is array} {
  146.     catch {unset a}
  147.     set x 27
  148.     set x [set a(foo) 11]
  149.     catch {unset a}
  150.     set x
  151. } 11
  152. test set-1.16 {TclCompileSetCmd: variable is array, elem substitutions} {
  153.     catch {unset a}
  154.     set i 5
  155.     set x 789
  156.     set a(foo5) 27
  157.     set x [set a(foo$i)]
  158.     catch {unset a}
  159.     set x
  160. } 27
  161.  
  162. test set-1.17 {TclCompileSetCmd: doing assignment, simple int} {
  163.     set i 5
  164.     set i 123
  165. } 123
  166. test set-1.18 {TclCompileSetCmd: doing assignment, simple int} {
  167.     set i 5
  168.     set i -100
  169. } -100
  170. test set-1.19 {TclCompileSetCmd: doing assignment, simple but not int} {
  171.     set i 5
  172.     set i 0x12MNOP
  173.     set i
  174. } {0x12MNOP}
  175. test set-1.20 {TclCompileSetCmd: doing assignment, in quotes} {
  176.     set i 25
  177.     set i "-100"
  178. } -100
  179. test set-1.21 {TclCompileSetCmd: doing assignment, in braces} {
  180.     set i 24
  181.     set i {126}
  182. } 126
  183. test set-1.22 {TclCompileSetCmd: doing assignment, large int} {
  184.     set i 5
  185.     set i 200000
  186. } 200000
  187. test set-1.23 {TclCompileSetCmd: doing assignment, formatted int != int} {
  188.     set i 25
  189.     set i 000012345     ;# an octal literal == 5349 decimal
  190.     list $i [incr i]
  191. } {000012345 5350}
  192.  
  193. test set-1.24 {TclCompileSetCmd: too many arguments} {
  194.     set i 10
  195.     catch {set i 20 30} msg
  196.     set msg
  197. } {wrong # args: should be "set varName ?newValue?"}
  198.  
  199. test set-2.1 {set command: runtime error, bad variable name} {
  200.     list [catch {set {"foo}} msg] $msg $errorInfo
  201. } {1 {can't read ""foo": no such variable} {can't read ""foo": no such variable
  202.     while executing
  203. "set {"foo}"}}
  204. test set-2.2 {set command: runtime error, not array variable} {
  205.     catch {unset b}
  206.     set b 44
  207.     list [catch {set b(123)} msg] $msg
  208. } {1 {can't read "b(123)": variable isn't array}}
  209. test set-2.3 {set command: runtime error, errors in reading variables} {
  210.     catch {unset a}
  211.     set a(6) 44
  212.     list [catch {set a(18)} msg] $msg
  213. } {1 {can't read "a(18)": no such element in array}}
  214. test set-2.4 {set command: runtime error, readonly variable} {
  215.     proc readonly args {error "variable is read-only"}
  216.     set x 123
  217.     trace var x w readonly
  218.     list [catch {set x 1} msg] $msg $errorInfo
  219. } {1 {can't set "x": variable is read-only} {can't set "x": variable is read-only
  220.     while executing
  221. "set x 1"}}
  222. test set-2.5 {set command: runtime error, basic array operations} {
  223.     list [catch {set a(other)} msg] $msg
  224. } {1 {can't read "a(other)": no such element in array}}
  225. test set-2.6 {set command: runtime error, basic array operations} {
  226.     list [catch {set a} msg] $msg
  227. } {1 {can't read "a": variable is array}}
  228.  
  229. catch {unset a}
  230. catch {unset b}
  231. catch {unset i}
  232. catch {unset x}
  233. return ""
  234.